home *** CD-ROM | disk | FTP | other *** search
/ Programming Sound Cards / Programming Sound Cards.iso / sound_29 / hld.c < prev    next >
C/C++ Source or Header  |  1995-01-01  |  4KB  |  160 lines

  1. // HLD.C - Horizontal Line Drawing
  2.  
  3. // Written by Phil Inch for Game Developers Magazine (issue 2).
  4. // Contributed to the public domain.
  5.  
  6. // This program written and compiled with Borland C++ v3.1
  7. // Compatibility with other compilers is not guaranteed.
  8.  
  9. // Usage of this program is subject to the disclaimer printed
  10. // in the magazine.  You assume all risks associated with the use
  11. // of this program.
  12.  
  13. // Note to experienced C programmers: I'm deliberately using 'double'
  14. // instead of 'int' to exaggerate the optimisation.  I don't know if
  15. // other languages optimise integer calculations as well as C does
  16. // so by doing this I hope to present a more general result.
  17.  
  18.  
  19.  
  20. #include <stdio.h>
  21. #include <conio.h>
  22. #include <stdlib.h>
  23. #include <time.h>
  24. #include <dos.h>
  25.  
  26.  
  27.  
  28. // Routine to swap to integers
  29. void swap( double a, double b ) {
  30.     double temp;
  31.     temp = a;
  32.     a = b;
  33.     b = temp;
  34. }
  35.  
  36.  
  37. // Routine to put screen into mode 13h
  38. void set_mode_13h( void ) {
  39.     asm {
  40.         mov ax,0x13
  41.         int 0x10
  42.         }
  43. }
  44.  
  45.  
  46. // Routine to put screen into mode 3h (text mode)
  47. void set_text_mode( void ) {
  48.     asm {
  49.         mov ax,0x03
  50.         int 0x10
  51.         }
  52. }
  53.  
  54.  
  55. // Slow horizontal line drawing program
  56. void slow_horizontal_line( double x1, double x2, double y, unsigned char colour ) {
  57.     char far *screen_location;
  58.     double xcount;
  59.  
  60.     for ( xcount = x1; xcount <= x2; xcount++ ) {
  61.         screen_location = MK_FP( 0xA000, ( (int)y*320 ) + (int)xcount );
  62.         *screen_location = colour;
  63.         }
  64. }
  65.  
  66.  
  67. // Fast horizontal line drawing program
  68. void fast_horizontal_line( double x1, double x2, double y, unsigned char colour ) {
  69.     char far *screen_location;
  70.     double xcount;
  71.  
  72.     screen_location = MK_FP( 0xA000, ((int)y*320) + (int)x1 );
  73.     for ( xcount = x1; xcount <= x2; xcount++ ) {
  74.         *screen_location = colour;
  75.         screen_location++;
  76.         }
  77. }
  78.  
  79.  
  80. // This is where the program first starts
  81. void main( void ) {
  82.     double x1, x2, y;
  83.     long lines, number_of_lines;
  84.     unsigned char colour;
  85.     char input[80];
  86.     clock_t slow_start, slow_end, slow_elapsed;
  87.     clock_t fast_start, fast_end, fast_elapsed;
  88.  
  89.     clrscr();
  90.  
  91.    printf( "*** HORIZONTAL LINE DRAWING DEMO ***\n\n" );
  92.  
  93.     printf( "Suggestions for number of lines:\n\n" );
  94.     printf( "486/50:  25000\n" );
  95.     printf( "486/33:  10000\n" );
  96.     printf( "486/25:   5000\n" );
  97.     printf( "386/40:    500\n" );
  98.     printf( "386/33:    200\n" );
  99.     printf( "lower :    100\n\n" );
  100.  
  101.     printf( "Enter number of lines to draw: " );
  102.     gets( input );
  103.  
  104.     number_of_lines = atol(input);
  105.     if ( number_of_lines <= 0 ) {
  106.         printf( "That's not a valid number!" );
  107.         exit(1);
  108.         }
  109.  
  110.     set_mode_13h();
  111.  
  112.     gotoxy( 18, 13 ); puts( "\"Slow\"" ); delay( 1500 );
  113.  
  114.     slow_start = clock();
  115.  
  116.     for ( lines = 0; lines < number_of_lines; lines++ ) {
  117.         x1 = rand()%320;        // 0 <= x1 <= 319
  118.         x2 = rand()%320;        // 0 <= x2 <= 319
  119.         y  = rand()%200;            // 0 <= y  <= 199
  120.         colour = rand()%256;        // 0 <= colour <= 255
  121.         if ( x1 > x2 ) swap( x1, x2 );
  122.         slow_horizontal_line( x1, x2, y, colour );
  123.         }
  124.  
  125.     slow_end = clock();
  126.     slow_elapsed = slow_end - slow_start;
  127.  
  128.     set_mode_13h();  // quick way of clearing the screen
  129.  
  130.     gotoxy( 18, 13 ); puts( "\"Fast\"" ); delay( 1500 );
  131.  
  132.     fast_start = clock();
  133.  
  134.     for ( lines = 0; lines < number_of_lines; lines++ ) {
  135.         x1 = rand()%320;        // 0 <= x1 <= 319
  136.         x2 = rand()%320;        // 0 <= x2 <= 319
  137.         y  = rand()%200;            // 0 <= y  <= 199
  138.         colour = rand()%256;        // 0 <= colour <= 255
  139.         if ( x1 > x2 ) swap( x1, x2 );
  140.         fast_horizontal_line( x1, x2, y, colour );
  141.         }
  142.  
  143.     fast_end = clock();
  144.     fast_elapsed = fast_end - fast_start;
  145.  
  146.     set_text_mode();
  147.  
  148.     printf( "\n\nTimes for %ld lines:\n", number_of_lines );
  149.  
  150.     printf( "\nSLOW:  Took %ld clock cycles (about %ld seconds)\n",
  151.         slow_elapsed, 5*(9+slow_elapsed)/91 );
  152.  
  153.     printf( "\nFAST:  Took %ld clock cycles (about %ld seconds)\n",
  154.         fast_elapsed, 5*(9+fast_elapsed)/91 );
  155.  
  156.     printf( "\nSAVING: FAST was %ld clock cycles (about %ld seconds) faster!\n\n",
  157.         slow_elapsed-fast_elapsed, 5*(9+slow_elapsed-fast_elapsed)/91 );
  158.  
  159. }
  160.